package mancala.ui;

import static mancala.Mancala.DEFAULT_HOUSE_STONE_COUNT;
import static mancala.Mancala.getImageIcon;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.Icon;
import javax.swing.SwingConstants;

import mancala.Mancala;
import mancala.listener.HouseMouseListener;
import mancala.model.House;
import mancala.model.Player;

public class HouseImpl extends House {

    private static final long serialVersionUID = 1L;
    private final Dimension minSize = new Dimension(65, 65);
    private int pitNr = -1;

    public HouseImpl(Mancala mancala, Player player, int pitNr) {
        setPlayer(player);
        setStones(DEFAULT_HOUSE_STONE_COUNT);
        this.pitNr = pitNr;

        addMouseListener(new HouseMouseListener(mancala));
        setFont(new Font("Arial", Font.PLAIN, 18));
        setHorizontalAlignment(SwingConstants.CENTER);
        setOpaque(false);
        final int id = player.getId() == 1 ? 2 : 1; // Reverse the player house position
        int x = 40 + pitNr * 93;
        int y = id * 53 + id * id * 53;
        if (id == 2) {
            setLocation(x, y);
        } else {
            setLocation(734 - x, y);
        }
        setSize(minSize);

    }

    @Override
    public Icon getIcon() {
        return getStones() < 12 ? getImageIcon("seeds_" + getStones() + ".png") : getImageIcon("seeds.png");
    }

    @Override
    public Dimension getMinimumSize() {
        return minSize;
    }

    @Override
    public Dimension getPreferredSize() {
        return minSize;
    }

    @Override
    public String toString() {
        return "HouseImpl[player=" + getPlayer().getName() + ", pit=" + pitNr + ", stones=" + getStones() + "]";
    }
}